package com.hmall.item.es;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.List;

public class ElesticAggsText {

    private RestHighLevelClient client;


    @BeforeEach
// 在每个测试方法之前执行
    void setUp() {
        // 创建客户端
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.253.130:9200")
        ));
    }


    @AfterEach
// 在每个测试方法之后执行
    void tearDown() throws IOException {
        // 关闭客户端
        this.client.close();
    }

    // 测试索引库的连接
    @Test
    public void testIndex(){
        System.out.println("hello client = " + client);
    }

    @Test
    public void testAggs() throws IOException {
        //1、创建请求
        SearchRequest request = new SearchRequest("market");
        //2、添加参数
        request.source().size(0);
        String TermsName = "brandAgg";
        request.source().aggregation(
                AggregationBuilders.terms(TermsName).field("brand.keyword")
                        .size(10)
        );
        //3、发送请求
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        //4、处理结果
        Aggregations aggregations = response.getAggregations();
        Terms bardAgg = aggregations.get(TermsName);
        List<? extends Terms.Bucket> buckets = bardAgg.getBuckets();
        for (Terms.Bucket bucket : buckets) {
            System.out.println("brand:" + bucket.getKeyAsString());
            System.out.println("count:" + bucket.getDocCount());
        }
    }

    @Test
    public void testAggsStart() throws IOException {
        //1、创建请求
        SearchRequest request = new SearchRequest("market");
        //2、添加参数
        request.source().size(0);

        String brandAggName = "brandAgg";
        request.source().aggregation(
                AggregationBuilders.terms(brandAggName).field("brand.keyword")
                        .size(10) //聚合三要素，类型，名称、排序字段
        );

        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

//        System.out.println("response=" + response);

        //4、处理结果
        Aggregations aggregations = response.getAggregations();
        Terms bardAgg = aggregations.get(brandAggName);
        List<? extends Terms.Bucket> buckets = bardAgg.getBuckets();
        for (Terms.Bucket bucket : buckets) {
            System.out.println("brand:" + bucket.getKeyAsString());
            System.out.println("count:" + bucket.getDocCount());
        }
    }



}
